import employees from '@/assets/constant/employees.js'
import store from '@/store'
export default {
  // 指令名:{生命周期}
  imgerror: {
    inserted(dom, obj, vnode) {
      dom.src = dom.src || obj.value
      dom.onerror = function() {
        dom.src = obj.value
      }
    },
    componentUpdated(dom, obj) {
      dom.src = dom.src || obj.value
    },
    unbind(dom, obj) {
      dom.onerror = null
    }
  },
  test: {},
  hireType: {
    inserted(dom, obj, vnode) {
      const result = employees.hireType.find((item) => {
        return item.id === obj.value
      })
      dom.innerText = result ? result.value : '-'
    },
    componentUpdated(dom, obj) {
      const result = employees.hireType.find((item) => {
        return item.id === obj.value
      })
      dom.innerText = result ? result.value : '-'
    }
  },
  // 点击dom以外执行一个回调方法
  clickoutside: {
    inserted(dom, obj, vnode) {
      const fn = (e) => {
        // 包含：判断dom包含关系   冒泡处理，拿到所有的冒泡路径做判断
        if (!dom.contains(e.target)) {
          const callback = obj.value
          callback()
        }
      }
      vnode.fn = fn
      document.addEventListener('click', fn)
    },
    unbind(dom, obj, vnode) {
      document.removeEventListener('click', vnode.fn)
    }
  },
  // 控制权限控制指令
  control: {
    /*
    bind:绑定时,
    inserted:添加到父节点，类似于mounted
    update:更新时，不保证更新完成
    componentUpdated:更新完成时
    unbind:解绑
    */
    inserted(dom, obj) {
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    },
    componentUpdated(dom, obj) {
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    }
  }
}
